/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.category;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.hutech09.jsale.bsn.ICategoryManager;
import org.hutech09.jsale.bsn.exception.ItemExistsException;
import org.hutech09.jsale.bsn.exception.ItemNotFoundException;
import org.hutech09.jsale.entity.Category;
import org.hutech09.jsale.entity.ProductType;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author Nam
 */
@ManagedBean
@SessionScoped
public class ProductTypeBrowseCtrl {

    @EJB
    private ICategoryManager cm;

    private Category category;

    private List<ProductType> types;

    private ProductType selectedType;

    private String aProductTypeName;

    public String getAProductTypeName() {
        return aProductTypeName;
    }

    public void setAProductTypeName(String aProductTypeName) {
        this.aProductTypeName = aProductTypeName;
    }

    public void aCreate() {
        if (category != null) {
            ProductType type = new ProductType();
            type.setName(aProductTypeName);
            type.setCategory(category);
            cm.create(type);
            setCategory(category); // reloading
        }
    }
    
    public void aUpdate(){
        if(selectedType != null){
            selectedType.setName(aProductTypeName);
            try {
                cm.update(selectedType);
            } catch (ItemExistsException ex) {
                Logger.getLogger(ProductTypeBrowseCtrl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void onRowSelect(SelectEvent event) {
    }

    public ProductTypeBrowseCtrl() {
    }

    public void delete() {
        if (selectedType != null) {
            if (cm.isEmpty(selectedType)) {
                try {
                    cm.deleteProductType(selectedType.getId());
                    setCategory(category);
                } catch (ItemNotFoundException ex) {
                    Logger.getLogger(ProductTypeBrowseCtrl.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                System.err.println("Selected tupe is not empty.");
            }
        }
    }

    void setCategory(Category selectedCategory) {
        this.category = cm.getCategory(selectedCategory.getId());
        try {
            types = cm.getAllProductType(category.getId());
        } catch (ItemNotFoundException ex) {
            types = null;
            Logger.getLogger(ProductTypeBrowseCtrl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List<ProductType> getTypes() {
        return types;
    }

    public void setTypes(List<ProductType> types) {
        this.types = types;
    }

    public ProductType getSelectedType() {
        return selectedType;
    }

    public void setSelectedType(ProductType selectedType) {
        this.selectedType = selectedType;
    }
}
